﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Compiler
{
    
    public partial class Form1 : Form
    {
        CompilerData CompilerDataobj;
        Commands commandsobj;
        public Form1()
        {
            InitializeComponent();
            commandsobj = new Commands();
            
        }


        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void result(int ErrorNum)
        {

            if (ErrorNum == 0)
            {
                ErrorList_txt.Text = " No Errors ...";
            }
            else if (ErrorNum != -1)
            {
                ErrorList_txt.Text = commandsobj.CompilerDataobj.Error_list[ErrorNum];
            }
            else
            {
                ErrorList_txt.Text = "";
                tokens_Lst.Items.Clear();
                identifier_Lst.Items.Clear();

            }

            if (ErrorNum != -1)
            {
                int Num = commandsobj.UserDataobj.Usertokens.Count;
                for (int i = 0; i < Num; i++)
                {
                    tokens_Lst.Items.Add(commandsobj.UserDataobj.Usertokens[i]);
                }

                Num = commandsobj.UserDataobj.IdentiferList.Count;
                for (int i = 0; i < Num; i++)
                {
                    identifier_Lst.Items.Add(commandsobj.UserDataobj.IdentiferList[i]);
                }

            }
        }
        private void CompileBtn_Click(object sender, EventArgs e)
        {
           
            result(-1);
            string code = CodeEditor_txt.Text;
            commandsobj = new Commands(code);
            int ErrorNum= commandsobj.CheckCode();
            result(ErrorNum);

           
        }

       

        private void loadBadExampleMenuItem_Click(object sender, EventArgs e)
        {
            StreamReader streamReader = new StreamReader("BadExample.txt");
            CodeEditor_txt.Text = streamReader.ReadToEnd();
            streamReader.Close();
        }

        private void example1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StreamReader streamReader = new StreamReader("GoodExample.txt");
            CodeEditor_txt.Text = streamReader.ReadToEnd();
            streamReader.Close();
        }

        private void example2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StreamReader streamReader = new StreamReader("GoodExample2.txt");
            CodeEditor_txt.Text = streamReader.ReadToEnd();
            streamReader.Close();
        }

        private void fileMenuItem_Click(object sender, EventArgs e)
        {
            //reset();
            result(-1);
        }

    }
}
